package org.poker.prophecy.simulation.gui;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Image;


import org.poker.prophecy.ImagesManager;
import org.poker.prophecy.core.Card;
import org.poker.prophecy.player.general.Player;
import org.poker.prophecy.simulation.Table;
import org.poker.prophecy.simulation.gui.TableSeat;
import org.poker.prophecy.simulation.process.ProcessType;
import org.poker.prophecy.simulation.process.RoundProcess;

public class TableGraphicTest extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;

	private static final int nbSeats = 8;
	
	private TableSeat[] seats = new TableSeat[nbSeats];
	private TableBoard board = null;
	
	private boolean playersHaveTakenSeats = false;
	
	public RoundProcess roundProcessRef = null;
	
	/**
	 * This is the default constructor
	 */
	public TableGraphicTest() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(1000, 600);
		this.setContentPane(getJContentPane());
		this.setTitle("The Poker Prophecy - Simulation Test");
		this.setAlwaysOnTop(true);
		this.setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			
			int[] gridxForSeats = {0,1,2,3,4,3,2,1};
			int[] gridyForSeats = {1,0,0,0,1,2,2,2};
			for(int i=0; i<8; i++){
				GridBagConstraints tmpGbc = new GridBagConstraints();
				tmpGbc.gridx = gridxForSeats[i];
				tmpGbc.gridy = gridyForSeats[i];
				
				this.seats[i] = new TableSeat();
				jContentPane.add(this.seats[i], tmpGbc);
				
			}
			
			GridBagConstraints tmpGbc = new GridBagConstraints();
			tmpGbc.gridx = 1;
			tmpGbc.gridy = 1;
			tmpGbc.gridwidth = 3;
			this.board = new TableBoard();
			jContentPane.add(this.board, tmpGbc);
			
		}
		return jContentPane;
	}


	public void doUpdates(Table table){
		
		//quick sleep to slow things down..
		quickSleep(500);

		//seat players on table 
		if(!this.playersHaveTakenSeats)
			seatPlayers(table);
		
		//if new hand clear stuff
		if(isStartingNewHand(table)){
			clearPlayerPocketCards();
			clearPlayerTalkBoxes();
			clearBoard();
		}		
		
		//update pocket cards
		updatePocketCards(table);
		
		//update stacks
		updatePlayerStacks(table);

		//update board
		updateBoard(table);

	}
	
	
	public boolean isStartingNewHand(Table table){
		if(roundProcessRef == null || roundProcessRef != table.getCurrentRoundProcess()){
			roundProcessRef = table.getCurrentRoundProcess();
			return true;
		}
		return false;
	}
	
	
	public void seatPlayers(Table table){
		
		int max = table.getPlayers().size();
		if(max > nbSeats) max = nbSeats;
		
		final Image image = ImagesManager.getPlayerImage("default");
        Icon icon = new ImageIcon(image);			
		
		for(int i=0; i < max; i++){
			seats[i].setPlayerName(table.getPlayers().elementAt(i).getName());
			seats[i].setPlayerIcon(icon);
			seats[i].setPlayerChips(table.getPlayers().elementAt(i).getMoney());
		}		
		
	}

	
	public void updatePlayerStacks(Table table){
		
		int max = table.getPlayers().size();
		if(max > nbSeats) max = nbSeats;
		
		for(int i=0; i < max; i++){
			seats[i].setPlayerChips(table.getPlayers().elementAt(i).getMoney());
		}		
	}

	
	public void updatePocketCards(Table table){
		
		int max = table.getPlayers().size();
		if(max > nbSeats) max = nbSeats;
		
		for(int i=0; i < max; i++){
			
			if(table.getCurrentRoundProcess() != null && table.getCurrentRoundProcess().hasFolded(table.getPlayers().elementAt(i))){
				seats[i].setPocketCards(null);
			}else{
				seats[i].setPocketCards(table.getPlayers().elementAt(i).getPocketCards());
			}
		}		
		
	}
	
	public void clearPlayerPocketCards(){
		for(int i=0; i < nbSeats; i++){
			seats[i].clearPocketCards();
		}		
	}
	
	
	
	public void playerTalk(Table table, Player player, String text){
		int max = table.getPlayers().size();
		if(max > nbSeats) max = nbSeats;		
		
		for(int i=0; i < max; i++){
			//TODO! compare player with slot.. or give players ids, .. or....
			if(player.getName().equals(table.getPlayers().elementAt(i).getName())){
				//shorten string for now,...
				if(text.length()>27) text = text.substring(0, 27);
				seats[i].setTalkBox(text);
			}
		}
	}

	
	public void clearPlayerTalkBoxes(){
		for(int i=0; i < nbSeats; i++){
			seats[i].setTalkBox(null);
		}		
	}
	
	
	public void updateBoard(Table table){
		if(table.getBoard() != null && table.getBoard().getFlop1() != null){
			Card c0 = table.getBoard().getFlop1();
			if(table.getBoard().getFlop1() != null)
				board.setCard(0,c0);

			Card c1 = table.getBoard().getFlop2();
			if(c1 != null)
				board.setCard(1,c1);

			Card c2 = table.getBoard().getFlop3();
			if(c2 != null)
				board.setCard(2,c2);

			Card c3 = table.getBoard().getTurn();
			if(c3 != null)
				board.setCard(3,c3);

			Card c4 = table.getBoard().getRiver();
			if(c4 != null)
				board.setCard(4,c4);
		}else{
			clearBoard();
		}
	}	

	
	
	public void clearBoard(){
		board.clearBoard();
	}

	
	public void quickSleep(long milisec){
		try {
			Thread.sleep(milisec);
		} catch (InterruptedException e){
			// lalala
		}
	}	
	
	
	

}

